﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace HvorBorMedlemmerne
{
    public partial class Form1 : Form
    {
        private string file= null;
        List<string> errormsg;
        string result = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Clipboard.SetText(txKML.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                file = openFileDialog1.FileName;
                if (file != null)
                {
                    result = "";
                    errormsg = new List<string>();
                    progressBar1.Value = 0;
                    // how many lines does the datafil contains?
                    string[] adresses = System.IO.File.ReadAllLines(file, System.Text.Encoding.GetEncoding("iso-8859-1"));
                    label2.Text = "Antal adresser: " + adresses.Length;
                    lbErrors.Items.Clear();
                    //disable the buttons
                    button1.Enabled = false;
                    button2.Enabled = false;
                    button3.Enabled = false;
                    // clear the result area
                    txKML.Text = "";
                    backgroundWorker1.RunWorkerAsync();
                    
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //save file to temp location
            System.IO.File.WriteAllText(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\temp.kml", txKML.Text);
            //open GE
            System.Diagnostics.Process.Start(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\temp.kml");
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            FjordagerGymnastikHvorBorMedlemmerne.ConvertAdressToCoordinate converter = new FjordagerGymnastikHvorBorMedlemmerne.ConvertAdressToCoordinate();
            List<string> errors;
            result = converter.GenerateKML(backgroundWorker1, file, out errors);
            foreach (var error in errors)
            {
                errormsg.Add(error);
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            txKML.Text = result;
            foreach (var item in errormsg)
            {
                lbErrors.Items.Add(item);    
            }
            button1.Enabled = true;
            button2.Enabled = true;
            button3.Enabled = true;            
        }

        
    }
}
